package com.jd.sunny.java;

import java.util.List;

/**
 * @Description:
 * @author:sunlin
 * @since:2012-12-04 15:02
 * @version:1.0.0
 */
public class TestInnerClass {

    public int age = 1000;

    public static void main(String[] args) {
        TestInnerClass testInnerClass = new TestInnerClass();
        // use OuterClass.InnerClass to reference innerClass
        //use OuterClass object .new InnerClass to create inner class
        TestInnerClass.InnerClass innerClass = testInnerClass.new InnerClass();
        testInnerClass.testLocalInnerClass();
    }

    /**
     * this code is auto generate

     private InnerClass outer;
     public TestInnerClass(InnerClass innerClass){
     outer = innerClass;
     }

     */

    /**
     * this is a inner class
     * only the inner class can be declared  as private
     */
    private class InnerClass {

        public void printAge(){
            //look out,here we can use outer class property
            //age == outer.age,but we can't see outer
            System.out.println(age);
            System.out.println(TestInnerClass.this.age);
        }
    }

    /**
     * test local inner class
     * the local inner class can't be declared public or private
     */
    private void testLocalInnerClass(){
        final int myMoney = 500;
        //this is wrong : LocalInnerClass localInnerClass = new LocalInnerClass();
        class LocalInnerClass{
            public void printMoney(){
                //in local inner class wo can only access final property because the local property's life cycle
                System.out.println("myMoney:" + myMoney);
                System.out.println("age:" + age);
            }
        }
        int money = 1000;
        //we must declare the LocalInnerClass first,and the wo can use it
        LocalInnerClass localInnerClass = new LocalInnerClass();
        localInnerClass.printMoney();
    }

    /**
     * test anonymos inner class
     * 
     */
    public void testAnonymosInnerClass(){

        //Cloneable
        Cloneable  cloneable = new Cloneable(){
            
        };

        //Comparable
        Comparable comparable = new Comparable(){
            public int compareTo(Object o) {
                return 0;  
            }
        };
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
